<template>
    <div class="card pos-2">
      <el-form :model="baseForm" label-width="auto" style="max-width:600px">
          <el-form-item label="速度">
            <el-input v-model.number="baseForm.speed" />
          </el-form-item>
          <el-form-item label="加速度">
            <el-input v-model.number="baseForm.accelerate" />
          </el-form-item>
          <el-form-item label="减速度">
            <el-input v-model.number="baseForm.decelerate" />
          </el-form-item>          
          <el-form-item>
            <div class="btn-pos">
                <el-button type="default" @mousedown="handleMove(0, false)" @mouseup="handleStop">X轴负向</el-button>
                <el-button type="default" @mousedown="handleMove(0, true)" @mouseup="handleStop">X轴正向</el-button>
             </div>
          </el-form-item>
          <el-form-item>
            <div class="btn-pos">
                <el-button type="default" @mousedown="handleMove(1, false)" @mouseup="handleStop">Y轴负向</el-button>
                <el-button type="default" @mousedown="handleMove(1, true)" @mouseup="handleStop">Y轴正向</el-button>
            </div>
          </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
import { reactive } from 'vue'
import { manualMoveApi, stopMoveApi } from '@/api/drive-api'
import { failedNotify, successNotify } from '@/comm/index.js'

const baseForm = reactive({
  speed: 10,
  accelerate: 500,
  decelerate: 500,
  axis: '0',
  direction: 'false'
})

function handleMove (axis, direction) {
  const payload = {
        Axis: parseInt(axis),
        Direction:Boolean(direction),
        Speed: baseForm.speed,
        Accelerate: baseForm.accelerate,
        Decelerate: baseForm.decelerate
      };

  manualMoveApi(payload, response => {
    successNotify('手动行驶成功');
    console.log('手动行驶成功');    
  }, error => {
    failedNotify('手动行驶失败');
    console.error('持续行驶失败', error);
  })
}

function handleStop() {
  stopMoveApi((response) => {
    successNotify('停止行驶成功');
    console.log('停止行驶成功');
  }, (error)=> {
    failedNotify('停止行驶失败');
    console.error('停止行驶失败', error);
  })
}
</script>

<style lang="scss" scoped>
.pos-2 {
  // margin-left: 5em;
  padding: 2.5em 1.5em 1.5em 1.5em;
}
.btn-pos {
  margin-left: 4em;
}
</style>